import {Component, OnInit} from '@angular/core';
import {ExtendHttpService} from "../../../common/extend-http.service";
import {Router} from "@angular/router";
import {TipService} from "../../services/tip.service";

@Component({
  selector: 'app-card-count',
  templateUrl: './card-count.component.html',
  styleUrls: ['./card-count.component.css']
})
export class CardCountComponent implements OnInit {

  loading = true;
  isLoading = false;
  isFlag = false;
  tipType = 'error';
  loadMoreFlag = true;
  list = [];
  totalPage = 1;
  params = {start: 1, name: '', mobile: '', cardNum: ''};
  result = {};

  constructor(private extendHttp: ExtendHttpService,
              private router: Router,
              private tipService: TipService) {
  }

  ngOnInit() {
    this.getBaseData();
    this.getData();
  }

  getBaseData() {
    this.extendHttp.post('/merchantApp/Statistics/cardStatistics', {}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.result= data.result;
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.loading = false;
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.loading = false;
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  getData(searchFlag?: boolean) {
    if (searchFlag) {
      // if (!this.params.startTime.trim()) {
      //   this.tipService.change(true, '请选择开始时间');
      //   return;
      // }
    }

    this.extendHttp.post('/merchantApp/Statistics/cardPage', this.params).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.list = data.page.list;
        this.params.start = data.page.pageNumber;
        this.totalPage = data.page.totalPage;
        this.isFlag = true;
        this.loading = false;
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.loading = false;
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.loading = false;
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }
  loadMore() {

    if (this.loadMoreFlag) {
      this.params.start = this.params.start + 1;
      this.loadMoreFlag = false;
      this.extendHttp.post('/merchantApp/Statistics/cardPage', this.params).subscribe(response => {
        let data = response.json();
        if (data.code == '0') {
          this.list = this.list.concat(data.page.list);
          this.params.start = data.page.pageNumber;
          this.totalPage = data.page.totalPage;
          this.loadMoreFlag = true;
        } else if (data.code == '-2001') {
          this.tipType = 'error';
          this.tipService.change(true, '登录超时，跳转到登录页面');
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 2000);
        } else {
          this.params.start = this.params.start - 1;
          this.tipType = 'error';
          this.tipService.change(true, data.message);
          this.loadMoreFlag = true;
        }
      }, error => {
        this.tipType = 'error';
        this.loadMoreFlag = true;
        this.params.start = this.params.start - 1;
        this.tipService.change(true, '系统繁忙，操作失败！');
      });
    }
  }
  clearData() {
    this.params.name = '';
    this.params.mobile = '';
    this.params.cardNum = ''
  }

}
